package com.nameli.admin.controller;

import com.nameli.SIM.bean.entity.*;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.SIM.util.tree.TreeNode;
import com.nameli.admin.service.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author NAME-L
 * @Description 通用显示内容
 * @className CommonController
 * @date 2021-11-05 10:46:01
 */
@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommonController {

    private static final Log log = LogFactory.getLog(CommonController.class);

    @Autowired
    private TechService techService;
    @Autowired
    private DepartMentService departMentService;
    @Autowired
    private FacultyService facultyService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private AreaService areaService;

    /**
     * 通用查询负责人并返回到前端页面
     * @return
     */
    @GetMapping("/positionName")
    public JsonResult queryByPoesition(@Param(value = "techPosition")Integer techPosition){
        JsonResult jsonResult = null;
        Map<String, Object> returnMap = new HashMap<>();
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        List<TeacherInfo> list = techService.queryByTechPosition(techPosition);
        if(list != null && list.size() > 0  ){
            log.info("check -----------> CommonController - queryByPoesition返回集合不为空");
            returnMap.put("list", list);
            errorCode = ReturnStatus.SUCCESS;
            errorMsg = "查询成功";
            jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnMap);
        }else {
            log.info("check -----------> CommonController - queryByPoesition返回集合为空");
            errorCode = ReturnStatus.ERROR;
            errorMsg = "查询失败";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }

    /**
     * 级联选择器返回给前端的格式内容
     *
     * @return 数据格式是 树形结构（此处不算正式的）
     * <p>
     * 结构详情：
     * List{
     * 学部内容
     * parentId:
     * parentName:
     * children:[{
     * 院系内容
     * parentId:
     * parentName:
     * children:[
     * 专业内容
     * parentId:
     * parentName:
     * ]
     * }
     * <p>
     * ]
     * }
     */
    @GetMapping("/treeData")
    public JsonResult treeToData() {
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息

        List<Map<String, Object>> list = new ArrayList<>();//首层list集合
        List<Map<String, Object>> departInfos = departMentService.queryByDepartMentNo();
        int departLength = departInfos.size(); //学部信息的集合长度
        for (int j = 0 ; j< departLength ; j++){
            Map<String, Object> firstMap = new HashMap<>();//首层树形Map集合，用来存放“学部 - 院系 - 专业”
            firstMap.put("parentId" , departInfos.get(j).get("departmentNo").toString());
            firstMap.put("parentName" ,departInfos.get(j).get("departmentName").toString());
            String pNode = departInfos.get(j).get("departmentNo").toString();//学部编号
            int count = facultyService.queryByDepartmentNo(pNode);//在院系表里通过学部编号查找数量，通过数量判断该学部下面是否有院系存在
//            log.info("院系表中"+departInfos.get(j).get("departmentName").toString()+"的学院总数 -> count：" + count);
            if (count > 0){//count>0说明存在院系
                List<Map<String, Object>> childList = new ArrayList<>();//第一层list集合
                List<Map<String, Object>> facultyInfos = facultyService.queryByDepartNo(pNode);
                int facultyLength = facultyInfos.size();//院系信息的集合长度
                for (int i = 0 ;i<facultyLength ; i++){
                    Map<String, Object> secondMap = new HashMap<>();//第二层Map集合，用来存放 “院系 - 专业”
                    secondMap.put("parentId" , facultyInfos.get(i).get("facultyNo").toString());
                    secondMap.put("parentName" ,facultyInfos.get(i).get("facultyName").toString());
                    String node =  facultyInfos.get(i).get("facultyNo").toString();
                    int SecondCount = majorService.queryCountByFacultyNo(node);
                    if (SecondCount > 0){
                        List<Map<String, Object>> thirdlist = new ArrayList<>();//第三层list集合
                        List<Map<String, Object>> childSecondList = majorService.queryByFacultyNo(node);
                        int majorLength = childSecondList.size();//专业信息的集合长度
                        for (int k = 0 ; k < majorLength ; k++){
                            Map<String, Object> thirdMap = new HashMap<>();//第三层Map集合，用来存放 “专业”
                            thirdMap.put("parentId" , childSecondList.get(k).get("majorNo"));
                            thirdMap.put("parentName" ,childSecondList.get(k).get("majorName"));
                            thirdlist.add(thirdMap);
                        }
                        secondMap.put("children", thirdlist);
                        childList.add(secondMap);
                    }
                }
                firstMap.put("children", childList);
                list.add(firstMap);
            }
        }
        errorCode = ReturnStatus.SUCCESS;
        errorMsg = "查询成功";
        jsonResult = JsonResult.returnMethod(errorCode, errorMsg, list);
        return jsonResult;
    }

    /**
     * 查询部门信息返给前端选择框
     * @return jsonResult
     */
    @GetMapping("/departMentName")
    public JsonResult  queryDepartMent(){
        JsonResult  jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息

        List<DepartmentInfo> infoList = departMentService.queryDepart();
        if( infoList != null && infoList.size() > 0 ){
            log.info("check -----------> CommonController - queryDepartMent返回集合不为空");
            errorCode = ReturnStatus.SUCCESS;
            errorMsg = "查询成功";
            jsonResult = JsonResult.returnMethod(errorCode, errorMsg, infoList);
        }else {
            log.info("check -----------> CommonController - queryDepartMent返回集合为空");
            errorCode = ReturnStatus.ERROR;
            errorMsg = "查询失败";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }

    /**
     * 学部 - 院系 树形结构返回前端级联选择器
     * @return
     */
    @GetMapping("/facultyData")
    public JsonResult facultyData(){
        log.info("enter -----------> 进入院系选择");
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息

        List<Map<String, Object>> list = new ArrayList<>();//首层list集合
        List<Map<String, Object>> departInfos = departMentService.queryByDepartMentNo();
        int departLength = departInfos.size(); //学部信息的集合长度
        for (int j = 0 ; j< departLength ; j++){
            Map<String, Object> firstMap = new HashMap<>();//首层树形Map集合，用来存放“学部 - 院系 - 专业”
            firstMap.put("parentId" , departInfos.get(j).get("departmentNo").toString());
            firstMap.put("parentName" ,departInfos.get(j).get("departmentName").toString());
            String pNode = departInfos.get(j).get("departmentNo").toString();//学部编号
            int count = facultyService.queryByDepartmentNo(pNode);//在院系表里通过学部编号查找数量，通过数量判断该学部下面是否有院系存在
//            log.info("院系表中"+departInfos.get(j).get("departmentName").toString()+"的学院总数 -> count：" + count);
            if (count > 0){//count>0说明存在院系
                List<Map<String, Object>> childList = new ArrayList<>();//第二层list集合
                List<Map<String, Object>> facultyInfos = facultyService.queryByDepartNo(pNode);
                int facultyLength = facultyInfos.size();//院系信息的集合长度
                for (int i = 0 ;i<facultyLength ; i++){
                    Map<String, Object> secondMap = new HashMap<>();//第二层Map集合，用来存放 “院系 - 专业”
                    secondMap.put("parentId" , facultyInfos.get(i).get("facultyNo").toString());
                    secondMap.put("parentName" ,facultyInfos.get(i).get("facultyName").toString());
                    childList.add(secondMap);
                }
                firstMap.put("children", childList);
                list.add(firstMap);
            }
        }
        errorCode = ReturnStatus.SUCCESS ;
        errorMsg = "查询成功" ;
        jsonResult = JsonResult.returnMethod(errorCode, errorMsg, list);
        return jsonResult;
    }

    /**
     * 返回最大的专业Id放到表单的MajorNo上
     * @return
     */
    @GetMapping("/maxMajorId")
    public JsonResult  maxMajorId() {
        JsonResult  jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        int maxId = majorService.selectMaxId();
        String majorNo = "M"+(maxId + 1);
        errorCode = ReturnStatus.SUCCESS;
        errorMsg = "生成成功";
        jsonResult = JsonResult.returnMethod(errorCode, errorMsg, majorNo);
        return jsonResult;
    }


    /**
     * 返回省市树形结构数据
     * @return
     */
    @GetMapping("/province")
    public JsonResult showAll(){
        JsonResult jsonResult = null;
        List<TreeNode<AreaInfo>> returnList =areaService.queryAll();
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        errorCode = ReturnStatus.SUCCESS;
        errorMsg = "查询成功";
        jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnList);
        return jsonResult;
    }
}
